package com.teamone.mobilewatch.chart;

import java.util.ArrayList;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.chart.PointStyle;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import com.teamone.mobilewatch.model.PatientReadings;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint.Align;

/**
 * PatientReadings chart
 */
public class PatientReadingsChart extends AbstractChart {
	private ArrayList<PatientReadings> readings;

	/**
	 * Constructs instance
	 * 
	 * @param readings
	 *            - array list of patient readings
	 */
	public PatientReadingsChart(ArrayList<PatientReadings> readings) {
		this.readings = readings;
	}

	/**
	 * Returns the chart name.
	 * 
	 * @return the chart name
	 */
	public String getName() {
		return "Recent Patient Readings";
	}

	/**
	 * Returns the chart description.
	 * 
	 * @return the chart description
	 */
	public String getDesc() {
		return "Patient readings - blood pressure, heart rate, body temperature.";
	}

	/**
	 * Executes the chart .
	 * 
	 * @param context
	 *            the context
	 * @return the built intent
	 */
	public Intent execute(Context context) {
		String[] titles = new String[] { "BPH", "BPL", "HR",
				"T" };
		
		// create array of x values 
		int numValues = readings.size();
		double[] xDim = new double[numValues];
		for(int i = 0; i < numValues; i++) {
			xDim[i] = i + 1;
		}
		
		List<double[]> x = new ArrayList<double[]>();
		for (int i = 0; i < titles.length; i++) {
			x.add(xDim);
		}

		// arrays of values
		List<double[]> values = new ArrayList<double[]>();
		double[] bphVal = new double[numValues];
		double[] bplVal = new double[numValues];
		double[] hrVal = new double[numValues];
		double[] tVal = new double[numValues];
		
		// iterate in reverse order
		;
		for (int i = 0, index = readings.size(); --index >= 0; i++) {
			PatientReadings pr = readings.get(index);
			bphVal[i] = pr.getBph();
			bplVal[i] = pr.getBpl();
			hrVal[i] = pr.getHr();
			tVal[i] = pr.getTemperature();
		}

		values.add(bphVal);
		values.add(bplVal);
		values.add(hrVal);
		values.add(tVal);
		
		
		int[] colors = new int[] { Color.BLUE, Color.GREEN, Color.CYAN,
				Color.YELLOW };
		PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE,
				PointStyle.DIAMOND, PointStyle.TRIANGLE, PointStyle.SQUARE };
		XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles);
		int length = renderer.getSeriesRendererCount();
		for (int i = 0; i < length; i++) {
			((XYSeriesRenderer) renderer.getSeriesRendererAt(i))
					.setFillPoints(true);
		}
		setChartSettings(renderer, "Patient Readings", "Time",
				"Value", 0, 11, 30, 130, Color.LTGRAY, Color.LTGRAY);
		renderer.setXLabels(12);
		renderer.setYLabels(10);
		renderer.setShowGrid(true);
		renderer.setXLabelsAlign(Align.RIGHT);
		renderer.setYLabelsAlign(Align.RIGHT);
		renderer.setZoomButtonsVisible(true);
		renderer.setPanLimits(new double[] { -10, 20, 10, 140 });
		renderer.setZoomLimits(new double[] { -10, 20, 10, 140 });

		Intent intent = ChartFactory.getLineChartIntent(context,
				buildDataset(titles, x, values), renderer,
				"Recent Patient Readings");
		return intent;
	}

}
